package com.elepant.tank;

import java.awt.*;
import java.util.Random;

public class Tank {

    public static int WIDTH = ResourceImage.goodTankU.getWidth();
    public static int HEIGHT = ResourceImage.goodTankU.getHeight();

    private int x = 0;
    private int y = 0;
    private Direction direction = Direction.Down;
    private static final int Speed = 2;

    //是否可以移动
    private boolean moving = true;

    //是否存活
    private boolean living = true;

    TankFrame tankFrame = null;

    private Group group = null;

    Rectangle rectangle = new Rectangle();//一个坦克对应一个矩形,然后在坦克移动后，更新矩形的坐标即可

    private Random random = new Random();

    public Tank(int x, int y, Direction direction, Group group, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.direction = direction;
        this.group = group;
        this.tankFrame = tankFrame;

        //初始化矩形
        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.width = WIDTH;
        rectangle.height = HEIGHT;
    }

    public void paint(Graphics graphics) {

        System.out.println("x:" + x + " y:" + y);
        /*Color c = graphics.getColor();
        graphics.setColor(Color.yellow);
        graphics.fillRect(x,y,50,50);
        graphics.setColor(c);*/

        /*x += 10;
        y += 10;*/

        if (!living) {
            this.tankFrame.tanks.remove(this);
        }

        switch (direction) {
            case Up:
                graphics.drawImage(this.group == Group.Bad ? ResourceImage.badTankU : ResourceImage.goodTankU, x, y, null);
                break;
            case Down:
                graphics.drawImage(this.group == Group.Bad ? ResourceImage.badTankD : ResourceImage.goodTankD, x, y, null);
                break;
            case Left:
                graphics.drawImage(this.group == Group.Bad ? ResourceImage.badTankL : ResourceImage.goodTankL, x, y, null);
                break;
            case Right:
                graphics.drawImage(this.group == Group.Bad ? ResourceImage.badTankR : ResourceImage.goodTankR, x, y, null);
                break;
            default:
                break;

        }

        moving();

    }

    private void moving() {
        if (!living) return;
        if (!moving) return;

        switch (direction) {
            case Up:
                y -= Speed;
                break;
            case Down:
                y += Speed;
                break;
            case Left:
                x -= Speed;
                break;
            case Right:
                x += Speed;
                break;

        }

        //边界检测
        boundaryDetection();

        //更新矩形坐标
        rectangle.x = this.x;
        rectangle.y = this.y;

        //敌人移动一步的时候，敌人顺便随机发射子弹
        if (this.group == Group.Bad && random.nextInt(100) > 98) {
            this.fire();
        }

        //让敌人随机方向移动
        if (this.group == Group.Bad && random.nextInt(100) > 95) {
            randomDirection();
        }
    }

    private void boundaryDetection(){
        if (this.x < 2) {
            this.x = 2;
        }
        if (this.y < 10) {
            this.y = 10;
        }
        if (this.x > TankFrame.ScreenWidth - Tank.WIDTH - 2) {
            this.x = TankFrame.ScreenWidth - Tank.WIDTH - 2;
        }
        if (this.y > TankFrame.ScreenHeight - Tank.HEIGHT - 2) {
            this.y = TankFrame.ScreenHeight - Tank.HEIGHT - 2;
        }
    }

    private void randomDirection() {
        this.direction = Direction.values()[random.nextInt(4)];
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public void fire() {
        int bX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int bY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;

        //根据不同发开火方向，微调子弹打出的位置
        switch (direction) {
            case Up:
                break;
            case Down:
                break;
            case Left:
                bX -= 4;
                bY += 3;
                break;
            case Right:
                bX += 4;
                bY += 3;
                break;
            default:
                break;

        }

        Bullet b = new Bullet(bX, bY, this.direction, this.group, tankFrame);
        tankFrame.bulletList.add(b);
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void die() {
        this.living = false;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }
}
